package com.atguigu.service.impl;

import com.atguigu.client.VodClient;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.domain.EduVideo;
import com.atguigu.service.EduVideoService;
import com.atguigu.mapper.EduVideoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
* @author Administrator
* @description 针对表【edu_video(课程视频)】的数据库操作Service实现
* @createDate 2024-06-06 10:09:33
*/
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo>
    implements EduVideoService{

    @Autowired
    private VodClient vodClient;
    //TODO 视频删除
    @Override
    public void removeVideoByCourseId(String id) {
        QueryWrapper<EduVideo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",id);
        queryWrapper.select("video_source_id");

        List<String> videoIds = new ArrayList<>();
        for (EduVideo eduVideo : baseMapper.selectList(queryWrapper)) {
            String videoSourceId = eduVideo.getVideoSourceId();
            if(!StringUtils.isEmpty(videoSourceId)){
                videoIds.add(videoSourceId);
            }
        }
        if(videoIds.size()>0){
            vodClient.deleteBatch(videoIds);
        }

        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",id);
        baseMapper.delete(wrapper);
    }
}




